<?php
/***
 * 人群Service
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */


namespace App\Services\CrowdService;
use Illuminate\Support\Facades\Log;
use Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class CrowdService
{
    /**
     * 添加验证
     * @param array $crowd
     * @return mixed
     */
    public function validator(array $crowd)
    {
        $v = [
            'crowd_name' => 'required|max:20',
            'description' => 'max:200',
        ];
        return  Validator::make(
            $crowd,$v

        );
    }
    /**
     * 添加人群
     * @param $crowd
     */
    public function addCrowd(array $crowd)
    {
        $crowd['state'] = 1;
        $crowd['created_at'] = time();
        $crowd['updated_at'] = time();
        $num = DB::table('crowds')->insert($crowd);
        return $num;
    }

    /**
     * 保存产品人群
     * @param $crowd_id
     * @param $product_temp_id
     */
    public function saveProductCrowd($crowd_id,$product_temp_id)
    {
        $productCrowd = $this->getProductCrowd($crowd_id,$product_temp_id);
        if($productCrowd==null)
        {
            $product_crowd['crowd_id']=$crowd_id;
            $product_crowd['product_id']=$product_temp_id;
            $product_crowd['state'] = 1;
            $product_crowd['created_at'] = time();
            $product_crowd['updated_at'] = time();
            DB::table('product_temp_crowds')->insert($product_crowd);
        }else{
            $this->updateProductCrowdState($crowd_id,$product_temp_id);
        }

    }

    /***
     * 获取产品模板下的所有映射人群
     * @param $product_temp_id
     * @return array
     */
    public function getProductCrowdByProductTempId($product_temp_id){
        $product_temp_id=trim($product_temp_id);
        if(empty($product_temp_id)){
            return array();
        }
        $product_temp_crowds= DB::table('product_temp_crowds')
            ->where(array('product_temp_id'=>$product_temp_id,'state'=>1))
            ->get()
            ->toArray();
        return $product_temp_crowds;
    }
    /**
     * 恢复已删除
     * @param $crowd_id
     * @param $product_temp_id
     * @return mixed
     */
    public function updateProductCrowdState($crowd_id,$product_temp_id)
    {
        return  DB::table('product_temp_crowds')->where('crowd_id',$crowd_id)->where('product_id',$product_temp_id)->update(['state' => 1]);
    }

    /**
     * 删除关系
     * @param $product_temp_id
     * @return mixed
     */
    public function deleteProductCrowdState($product_temp_id)
    {
        return  DB::table('product_temp_crowds')->where('product_id',$product_temp_id)->update(['state' => 2]);
    }

    /**
     * 通过人群名称获取人群
     * @param $crowd_name
     * @return mixed
     */
    public function getCrowdForName($crowd_name){
        $crowd =  DB::table('crowds')->where('crowd_name',$crowd_name)->where('state',1)->first();
        return $crowd;
    }

    /***
     * 通过人群Id获取人群
     * @param $crowd_id
     * @return string
     */
    public function getCrowdNameById($crowd_id){
        if(empty($crowd_id)){
            return '';
        }
        $crowd_name=DB::table('crowds')->where(array('id'=>$crowd_id))->value('crowd_name');
        return empty($crowd_name)?'':$crowd_name;
    }
    /**
     * 除当前人群外是否有重复人群名称
     * @param string $crowd_name
     * @return mixed
     */
    public function getCrowdForNameIsNotCurrent($crowd_id,$crowd_name){
        $crowd =  DB::table('crowds')->where('crowd_name',$crowd_name)->where('id','<>',$crowd_id)->where('state','<>',2)->first();
        return $crowd;
    }

    /**
     * 获取人群列表
     * @param Request $request
     */
    public function getCrowdList(Request $request)
    {
        $search = $request->input('search','');
        $crowds = DB::table('crowds')->where('crowd_name','like','%'.$search.'%')->where('state','<>',2)->paginate(8);
        $crowds->appends(['search' => $search])->render();
        $crowds->search = $search;
        return $crowds;
    }

    /***
     * 获取所有人群
     * @param Request $request
     * @return mixed
     */
    public function getAllCrowdList(Request $request)
    {
        $search = trim($request->input('search',''));
        $crowds = DB::table('crowds')->where('state',1)->where('crowd_name','like','%'.$search.'%')->get();
        return $crowds;
    }

    /***
     * 删除人群
     * @param $crowd_id
     * @return mixed
     */
    public function deleteCrowd($crowd_id){
        $num =  DB::table("crowds")->where('id', $crowd_id) ->update(['state' => 0,'updated_at'=>time()]);
        return $num;
    }

    /**
     * 修改人群
     * @param array $crowd
     * @return mixed
     */
    public function updateCrowd(array $crowd){
        $crowd['updated_at'] = time();
        $num =  DB::table("crowds")->where('id', $crowd['id'])->update($crowd);
        return $num;
    }

    /**
     * 根据ID获取未删除的人群
     * @param $crowd_id
     * @return mixed
     */
    public function getCrowdForId($crowd_id)
    {
        return DB::table("crowds")->where('id',$crowd_id)->where('state','<>',2)->first();
    }

}